package hongcaosp.app.android.contact;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.ImageViewTarget;

import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.bean.Message;
import hongcaosp.app.android.modle.bean.SM;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.utils.DateUtil;
import xlj.lib.android.base.view.CircleImageView;
import xlj.lib.android.base.view.ProxyAdapter;

public class MessageFragment extends BaseFragment implements IListView<Message> {
    private RecyclerView recyclerView;
    private MessageAdapter messageAdapter;
    private MessagePresenter messagePresenter;


    @Override
    public void initView() {
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayout.VERTICAL, false));

        View headView = getLayoutInflater().inflate(R.layout.header_message, null);
        messageAdapter = new MessageAdapter(getContext());
        ProxyAdapter proxyAdapter = new ProxyAdapter(messageAdapter);
        proxyAdapter.addHeaderView(headView);
        recyclerView.setAdapter(proxyAdapter);
        messagePresenter = new MessagePresenter(this,0);
        messagePresenter.freshMessage();

        headView.findViewById(R.id.btn_at).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListActivity.toActivity(getContext(),ListActivity.LIST_TYPE_ATME);
            }
        });

        headView.findViewById(R.id.btn_comment).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ListActivity.toActivity(getContext(),ListActivity.LIST_TYPE_COMMENT);
            }
        });

        headView.findViewById(R.id.btn_good).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListActivity.toActivity(getContext(),ListActivity.LIST_TYPE_GOODER);
            }
        });

        headView.findViewById(R.id.btn_fan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListActivity.toActivity(getContext(),ListActivity.LIST_TYPE_FANS);
            }
        });

        findViewById(R.id.contact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(),ContactActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onShow() {
        super.onShow();
        if (messagePresenter != null) {
            messagePresenter.freshMessage();
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_message;
    }

    @Override
    public void freshList(List<Message> dataList, boolean canLoadMore) {
        messageAdapter.msgDataList.clear();
        if(dataList != null) {
            messageAdapter.msgDataList.addAll(dataList);
        }
        messageAdapter.notifyDataSetChanged();
    }

    @Override
    public void addList(List<Message> dataList,boolean canLoadMore) {
        if(dataList != null) {
            messageAdapter.msgDataList.addAll(dataList);
        }
        messageAdapter.notifyDataSetChanged();
    }

    class MessageAdapter extends RecyclerView.Adapter<MessageHolder> {
        private List<Message> msgDataList;
        private Context context;

        public MessageAdapter(Context context) {
            this.context = context;
            msgDataList = new ArrayList<>();
        }

        @NonNull
        @Override
        public MessageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MessageHolder(getLayoutInflater().inflate(R.layout.item_message,parent,false), context);
        }

        @Override
        public void onBindViewHolder(@NonNull MessageHolder holder, int position) {
            Message messageData = msgDataList.get(position);
            holder.setMessageData(messageData);
        }

        @Override
        public int getItemCount() {
            return msgDataList.size();
        }
    }

    class MessageHolder extends RecyclerView.ViewHolder {
        private CircleImageView logoIV;
        private TextView nameTV, desTV, timeTV;
        private Context context;
        private Message message;

        public MessageHolder(View itemView, Context context) {
            super(itemView);
            this.context = context;

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (message != null) {
                        IMActivity.toIM(getActivity(),message.getNickname(),"" + UserToken.getDefault().getUser().getId(), "" + message.getId());
                    }
                }
            });
            logoIV = itemView.findViewById(R.id.user_iv);
            logoIV.setBorderWidth(3);
            nameTV = itemView.findViewById(R.id.name);
            desTV = itemView.findViewById(R.id.descrip);
            timeTV = itemView.findViewById(R.id.time);
        }

        public void setMessageData(Message messageData) {
            this.message = messageData;
            SM sm = messageData.getSm();
            if (sm != null) {
                setData(messageData.getImage(),messageData.getNickname(),sm.getContent(),DateUtil.getChatTimeStr(sm.getSendTime()));
            } else {
                setData(messageData.getImage(), messageData.getNickname(), messageData.getDes(), messageData.getTime());
            }
        }

        public void setData(final String logoUrl, String name, String des, String time) {
            Glide.with(context).load(logoUrl).placeholder(R.drawable.icon_head).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new ImageViewTarget<GlideDrawable>(logoIV) {
                @Override
                protected void setResource(GlideDrawable resource) {
                    logoIV.setImageDrawable(resource);
                }
            });

            if (TextUtils.isEmpty(name)) {
                nameTV.setText("-");
            } else {
                nameTV.setText(name);
            }
            if (TextUtils.isEmpty(des)) {
                desTV.setText("-");
            } else {
                desTV.setText(des);
            }
            if (TextUtils.isEmpty(time)) {
                timeTV.setText("-");
            } else {
                timeTV.setText(time);
            }
        }
    }
}
